var isIe=(document.all)?true:false;

function setSelectState(state)
{
var objl=document.getElementsByTagName('select');
for(var i=0;i<objl.length;i++)
{
objl[i].style.visibility=state;
}
}
//点击下载时进行判断cookie是不是存在
//设置cookie的值
function SetCookie (name, value) {
       var exp = new Date();
       exp.setTime (exp.getTime()+864000);
       document.cookie = name + "=" + value + "; expires=" + exp.toGMTString()+"; path=/";
}

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function DelCookie(name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}

function GetCookie(name) {
         var arg = name + "=";
         var alen = arg.length;
         var clen = document.cookie.length;
         var i = 0;
         while (i < clen) {
             var j = i + alen;
             if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
             i = document.cookie.indexOf(" ", i) + 1;
             if (i == 0) break;
         }
      return null;
}

function getScrollTop()
{
    if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
	}else
	{
	   return document.body.scrollTop;
	}
}

//弹出要求输入email的框
function showregister(_this){

	var bWidth=parseInt(document.documentElement.scrollWidth);
	var bHeight=parseInt(document.documentElement.scrollHeight);
	var tablewidth=400;
	var tableheight=300;
	var clientHeight=parseInt(document.documentElement.clientHeight);
	if(!document.getElementById("back")){
		var back=document.createElement("div");
		back.id="back";
		var styleStr="z-index:997;top:0px;left:0px;position:absolute;background:#666;width:"+bWidth+"px;height:"+bHeight+"px;";
		styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;";
		back.style.cssText=styleStr;
		document.body.appendChild(back);
		showBackground(back,50);
	}

	styleStr="left:"+(bWidth-tablewidth)/2+"px;top:"+(getScrollTop()+(clientHeight-tableheight)/2)+		"px;position:absolute;z-index:999;";
	document.getElementById("registerWindow").style.cssText=styleStr;
	$("#registerWindow").css("display","block");

}

function showdetail(effectname,thrumburl,protype,hasvideo,videourl,author,requirever,submitdate,downloadurl)
{
closeWindow2();
var bWidth=parseInt(document.documentElement.scrollWidth);
var bHeight=parseInt(document.documentElement.scrollHeight);
if(isIe){
var curscrolltop=document.documentElement.scrollTop;
setSelectState('hidden');
}
if(protype==0) //free download
{
 var downloadpic="freedownload.gif";
}else
{
 var downloadpic="prodownload.gif";
}
var back=document.createElement("div");
back.id="back";
var styleStr="z-index:997;top:0px;left:0px;position:absolute;background:#666;width:"+bWidth+"px;height:"+bHeight+"px;";
styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;";
back.style.cssText=styleStr;
document.body.appendChild(back);
showBackground(back,50);
//
var host = "http://"+window.location.hostname;
var clientHeight=parseInt(document.documentElement.clientHeight);
var styleStr2="z-index:999";
var mesW=document.createElement("div");
mesW.id="mesWindow";
mesW.className="mesWindow";
var tablewidth=460;
var tableheight=464;
if(hasvideo==1)//是视频介绍
{
var divwidth=460;
var divheight=470;
var tablewidth=420;
var effectnameleft=18;
var descurl="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='392px' height='294px'>"+
      "<param name='movie' value='vcastr22.swf?vcastr_file="+ videourl +"&LogoText=&IsAutoPlay=0'>"+
      "<param name='quality' value='high'>"+
      "<param name='wmode' value='transparent'>"+
      "<param name='allowFullScreen' value='true'>"+
      "<embed src='"+host+"/effect/vcastr22.swf?vcastr_file="+ videourl +"&LogoText=&IsAutoPlay=0' allowfullscreen='true' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='392px' height='294px'>"+
      "</object>";
}else //图片介绍
{
var divwidth=420;
var divheight=410;
var tablewidth=410;
var effectnameleft=3;

var descurl="<div style='border:5px solid #0099CC; width:320px; height:240px; background:#FFFFFF;' class='websjy2'><img src='"+videourl+"' width='320' height='240'/></div>";
}
mesW.innerHTML="<div style=' border:1px solid #0099CC; width:"+divwidth+"px; height:"+divheight+"px; background:#FFFFFF;' class='websjy2'><div style='border:0px solid #0099CC; width:"+divwidth+"px; height:20px; background:#FFFFFF;' class='websjy2'><table width='"+divwidth+"' border='0'><tr><td width='390' align='left'><div style='padding-left:"+effectnameleft+"px'><strong>"+effectname+"</strong></div></td><td width='20' align='right'><span style='cursor:pointer'><a onclick= 'closeWindow();'><img src='"+host+"/Public/home/images/close.png' width='20' height='20' /></a></span></td></tr></table></div><table width="+tablewidth+"  border='0' align='center' style='margin:0 auto;'><tr><td colspan='3' align='center'>"+descurl+"</td></tr><tr><td colspan='3'><img src='"+host+"/Public/home/images/fenge.gif' width='400' height='9' /></td></tr><tr><td width='82' rowspan='3'><img src='"+thrumburl+"' width='79' height='59' /></td><td width='45' height='18' align='right'>&nbsp;</td><td width='285' align='left'><strong>Submitted by: </strong>"+author+"</td></tr><tr><td height='19' align='right'>&nbsp;</td><td width='285' align='left'><strong>Requires version: </strong>"+requirever+" or newer</td></tr><tr><td height='15' align='right'>&nbsp;</td><td width='285' align='left'><strong>Date: </strong>"+submitdate+"</td></tr><tr><td>&nbsp;</td><td align='right'>&nbsp;</td><td align='right'><a href='javascript:;'><img onclick='IsSetcookie(this)'; durl='"+downloadurl+"' src='"+host+"/Public/home/images/"+downloadpic+"' width='152' height='27' border='0' /></a></td></tr></table></div>";
styleStr="left:"+(bWidth-tablewidth)/2+"px;top:"+(getScrollTop()+(clientHeight-tableheight)/2)+"px;position:absolute;";
//styleStr="left:"+(bWidth-tablewidth)/2+"px;top:500px;position:absolute;";
//styleStr="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;";
styleStr +="z-index:998;";
mesW.style.cssText=styleStr;
document.body.appendChild(mesW);
}
//让背景渐渐变暗
function showBackground(obj,endInt)
{
  if(isIe)
  {
    obj.filters.alpha.opacity+=1;
    if(obj.filters.alpha.opacity<endInt)
    {
      setTimeout(function(){showBackground(obj,endInt)},5);
    }
  }else{
    var al=parseFloat(obj.style.opacity);al+=0.01;
    obj.style.opacity=al;
    if(al<(endInt/100))
    {

	   setTimeout(function(){showBackground(obj,endInt)},5);
	}
  }
}
//关闭窗口
function closeWindow()
{
if(document.getElementById('back')!=null)
{
document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
}
if(document.getElementById('mesWindow')!=null)
{
document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow'));
}
if(isIe){
setSelectState('');}
}
//关闭窗口
function closeWindow2()
{
if(document.getElementById('back')!=null)
{
document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
}
if(document.getElementById('registerWindow').style.display!="none")
{
document.getElementById('registerWindow').style.display = "none";
}
if(isIe){
setSelectState('');}
}
function checkemail(email)
{
   //对电子邮件的验证
   var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
   if(!myreg.test(email))
   {
        alert('提示\n\n请输入有效的E_mail！');
        return false;
  }else return true;
}
$(function(){
	$('#sub_btn').click(function(){
		var  email = $('input#uname').val();
		var durl = $(this).attr('durl');
		if(!checkemail(email)){
			return false;
		}
		//alert($(this).parents('form').serialize());return false;

		var url="http://"+window.location.hostname+"/index.php/Index/email";
		//$(this).parents('form').attr('action',url);
		$.post(url,{'email':email},function(ret){
			if(ret==1 || ret==2){
				SetCookie('down','local');
				closeWindow2();
				window.location = 'http://' + window.location.host + '/' + durl;
				return;
			}else{
			alert('失败');
			}
		});

	});
	$('#uname').keydown(function(e){
		if(e.keyCode==13){
		var  email = $('input#uname').val();
		var durl = $(this).attr('durl');
		if(!checkemail(email)){
			return false;
		}
		//alert($(this).parents('form').serialize());return false;

		var url="http://"+window.location.hostname+"/index.php/Index/email";
		//$(this).parents('form').attr('action',url);
		$.post(url,{'email':email},function(ret){
			if(ret==1 || ret==2){
				SetCookie('down','local');
				closeWindow2();
				window.location = 'http://' + window.location.host + '/' + durl;
				return;
			}else{
			alert('失败');
			}
		});
		}
	});

})

function IsSetcookie(_this){
	var durl = $(_this).attr('durl'); 
	$('#uname').attr('durl',durl);
	$('#sub_btn').attr('durl',durl);
	var cval = GetCookie('down');
	if(cval == 'local'){
		window.location = 'http://' + window.location.host + '/' + durl;
	}
	else{
		showregister();
	}
}

